package com.jm3.riscar3.alu;

import com.jm3.riscar3.core.CPU;
import com.jm3.riscar3.core.Microcontroller;
import com.jm3.riscar3.interfaces.IAR3Command;
import com.jm3.riscar3.memory.Memory;
import com.jm3.riscar3.memory.Register;
import com.jm3.riscar3.memory.StatusRegister;

public class RLC implements IAR3Command {
	
	public CPU cpu;
	public Memory memory;

	public RLC(Microcontroller mcu) {
		this.cpu = mcu.getCPU();
		this.memory = mcu.getMemory();
	}

	@Override
	public void execute(String args) throws Exception {
		// TODO Auto-generated method stub
		Register acc = cpu.getAcc();
		int accValue = acc.readRegister();
		int result = accValue << 1;
		StatusRegister sr = cpu.getSR();
		if (sr.getCarryFlag())
			result++;

		try {
			if (Integer.highestOneBit(result) == ALUUtils.NINTH_BIT)
				sr.setCarryFlag(true);
			else
				sr.setCarryFlag(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		acc.writeRegister(result & ALUUtils.EIGHT_BIT_MASK);
		
		//Set flags
		cpu.getSR().setZeroFlag(ALUUtils.isZero(result));
		cpu.getSR().setNegativeFlag(ALUUtils.isEightBitNegative(result));
		
	}

	@Override
	public String getOpCode() {
		// TODO Auto-generated method stub
		return "01000";
	}

}
